﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetX;

namespace RPGXEngine
{
    public class TypeManager
    {
        #region Public Methods

        public object AddValues(object left, object right)
        {
            object result = left;

            Type leftType = left.GetType();
            Type rightType = right.GetType();

            if (leftType == typeof(int))
            {
                if (rightType == typeof(string))
                {
                    if (ConvertExtensions.IsInt32((string)right))
                    {
                        int r = Convert.ToInt32(right);
                        int l = (int)left;

                        l += r;

                        result = l;
                    }
                }
                else if (rightType == typeof(int))
                {
                    int l = (int)left;
                    int r = (int)right;

                    result = l + r;
                }
            }
            else if (leftType == typeof(string))
            {
                string s = (string)left;

                s += Convert.ToString(right);

                result = s;
            }

            return result;
        }

        public object MinusValues(object left, object right)
        {
            object result = left;

            Type leftType = left.GetType();
            Type rightType = right.GetType();

            if (leftType == typeof(int))
            {
                if (rightType == typeof(int))
                {
                    int l = (int)left;
                    int r = (int)right;

                    result = l - r;
                }
            }
            else if (leftType == typeof(string))
            {
                string l = (string)left;
                string r = right.ToString();

                if (l.EndsWith(r))
                    l = l.Remove(l.Length - r.Length);

                result = l;
            }

            return result;
        }

        public object MultiplyValues(object left, object right)
        {
            object result = left;

            Type leftType = left.GetType();
            Type rightType = right.GetType();

            if (leftType == typeof(int))
            {
                if (rightType == typeof(int))
                {
                    int l = (int)left;
                    int r = (int)right;

                    result = l * r;
                }
            }

            return result;
        }

        public object DivideValues(object left, object right)
        {
            object result = left;

            Type leftType = left.GetType();
            Type rightType = right.GetType();

            if (leftType == typeof(int))
            {
                if (rightType == typeof(int))
                {
                    int l = (int)left;
                    int r = (int)right;

                    result = l / r;
                }
            }

            return result;
        }

        #endregion
    }
}
